﻿using UnityEngine;
using System.Collections;

public class TweenMovePos : MonoBehaviour {
	#region member
	public Transform MoveGA;
	[SerializeField]bool _isPlayX;
	[SerializeField]bool _isPlayY;
	[SerializeField]float startPos;
	[SerializeField]float targerPos;
	[SerializeField]float _moveTime = 1;
	[SerializeField]float _moveOutTime = 0.2f;
	float _startPos = 0;
	float _moveTarget = -300;
	float saveTime;
	float movetime = 1;
	public AnimationCurve moveAni = AnimationCurve.Linear(0,0,1,1);
	// Use this for initialization
	#endregion
	#region interface
	public void MoveXIn(){
		movetime = _moveTime;
		_startPos = startPos;
		_moveTarget = targerPos;
		_isPlayX = true;
		saveTime = Time.time;
	}

	public void MoveXOut(){
		movetime = _moveOutTime;
		_moveTarget = startPos;
		_startPos = targerPos;
		_isPlayX = true;
		saveTime = Time.time;
	}

	public void MoveYIn(){
		movetime = _moveTime;
		_startPos = startPos;
		_moveTarget = targerPos;
		_isPlayY = true;
		saveTime = Time.time;
	}

	public void MoveYOut(){
		movetime = _moveOutTime;
		_moveTarget = startPos;
		_startPos = targerPos;
		_isPlayY = true;
		saveTime = Time.time;
	}

	#endregion

	#region 方法
	void MoveX(float time){
		Vector3 pos = MoveGA.localPosition;
		pos.x = Mathf.Lerp (_startPos, _moveTarget,moveAni.Evaluate(time));
		MoveGA.localPosition = pos;
	}

	void MoveY(float time){
		Vector3 pos = MoveGA.localPosition;
		pos.y = Mathf.Lerp (_startPos, _moveTarget, moveAni.Evaluate(time));
		MoveGA.localPosition = pos;
	}
	void Update(){
		if (_isPlayX) {
			float t = Mathf.Clamp ((Time.time - saveTime) / movetime, 0, 1);
			MoveX (t);
			if (t == 1) {
				_isPlayX = false;
			}
		}
		if (_isPlayY) {
			float y = Mathf.Clamp ((Time.time - saveTime) / movetime, 0, 1);
			MoveY (y);
			if (y == 1) {
				_isPlayY = false;
			}
		}
	}
	public void LateUpdate(){
		if (!_isPlayX) {
			if (moveXIn) {
				moveXIn = false;
				MoveXIn ();
			}
			if (moveXOut) {
				moveXOut = false;
				MoveXOut ();
			}
		}
		if (!_isPlayY) {
			if (moveYIn) {
				moveYIn = false;
				MoveYIn ();
			}
			if (moveYOut) {
				moveYOut = false;
				MoveXOut ();
			}
		}
	}
	public bool moveXIn;
	public bool moveYIn;
	public bool moveXOut;
	public bool moveYOut;
	#endregion
}
